<?php
/*\
 * Copyright 2012 Rob van de Werken
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
\*/

define("PAGE","TVgXML.php");
define("FILE","xmltv.xml");

function output_program($program,$cur=false,$next=false){
	if($next){
		echo "<td class='TVg-change'>".substr($program['start'],0,2).":".substr($program['start'],2,2)."</td>";
	}else{
		echo "<td class='TVg-start'>".substr($program['start'],0,2).":".substr($program['start'],2,2)."</td>";
	}
	echo "<td><div class='TVg-title'>".$program['title']."<div class='tooltip'>".
		"<div class='category' onclick='navigationGoto(\"".PAGE."?category=".urlencode($program['category'])."\");'>".$program['category']."</div>".
		"<div class='desc'>".nl2br($program['desc'])."</div>".
		"</span></div></td>\n";
	if($cur){
		echo "<td class='TVg-stop'></td>\n";
	}else{
		 echo "<td class='TVg-stop'>".substr($program['stop'],0,2).":".substr($program['stop'],2,2)."</td>";
	}
}

//Load xmltv file
if(file_exists(FILE)){
    $xml = simplexml_load_file(FILE);
}else{
    echo "Required xmltv.xml not found.";
}

if($xml){
	//Generate channel-id translation array
	foreach($xml->channel as $channel){
		$channels[(string)$channel->attributes()->id]=(string)$channel->{"display-name"};
	}

	foreach($xml->programme as $programme){
		$programs[$channels[(string)$programme->attributes()->channel]][substr((string)$programme->attributes()->start,0,14)] = array(
			"start"		=> substr((string)$programme->attributes()->start,8,4),
			"stop"		=> substr((string)$programme->attributes()->stop,8,4),
			"title"		=> (string)$programme->title,
			"desc"		=> trim((string)$programme->desc),
			"category"	=> (string)$programme->category
		);
	}

	if(isset($_REQUEST['category'])){
		$date="";
		//Generate overview for a category (default film)
		foreach($programs as $channelname => $channel){
			foreach($channel as $stamp=>$program){
				if($program['category']==$_REQUEST['category']){
					$cat[$stamp]=$program;
					$cat[$stamp]['channel']=$channelname;
				}
			}
		}

		if(isset($cat)){
			echo "<div class='head' onclick='navigationGoto(\"".PAGE."\");'>".$_REQUEST['category'].":</div>\n".
				 "<table class='TVg-cur'>";
			ksort($cat);
			foreach($cat as $stamp=>$program){
				if($date!=substr($stamp,0,8)){
					$date=substr($stamp,0,8);
					echo "<tr><th colspan='4' class='TVg-date'>".strftime("%A, %B %e, %Y",strtotime($stamp))."</th></tr>\n";
				}

				echo "<tr><td onclick='navigationGoto(\"".PAGE."?channel=".urlencode($program['channel'])."\");' class='TVg-chan'>".$program['channel']."</td>";
				output_program($program);
				echo "</tr>\n";
			}
		}else{
			echo "No broadcasts found for specified category.";
		}
	}elseif(isset($_REQUEST['channel'])){
		$date="";
		if(array_key_exists($_REQUEST['channel'],$programs)){
			echo "<div class='head' onclick='navigationGoto(\"".PAGE."\");'>".$_REQUEST['channel']."</div>\n".
				 "<table class='TVg-day'>";

			ksort($programs[$_REQUEST['channel']]);
			foreach($programs[$_REQUEST['channel']] as $stamp=>$program){
				if($date!=substr($stamp,0,8)){
					$date=substr($stamp,0,8);
					echo "<tr><th colspan='3' class='TVg-date'>".strftime("%A, %B %e, %Y",strtotime($stamp))."</th></tr>\n";
				}

				echo "<tr>";
				output_program($program);
				echo "</tr>";
			}
		}else{
			echo "<div onclick='navigationGoto(\"".PAGE."\");'>Error, channel not found.</div>";
		}
	}else{
		ksort($programs);
		//Generate current and next show overview
		echo "<div class='head' onclick='navigationGoto(\"".PAGE."?category=Film\");'>Current:</div>\n".
			 "<table class='TVg-cur'>\n";
		foreach($programs as $channelname => $channel){
			ksort($channel);
			foreach($channel as $stamp=>$program){
				if($stamp>strftime("%Y%m%d%H%M%S")){
					$next=$stamp;
					break;
				}else{
					$cur=$stamp;
				}
			}
			echo "<tr><td onclick='navigationGoto(\"".PAGE."?channel=".urlencode($channelname)."\");' class='TVg-chan'>".$channelname."</td>";
			if(!isset($cur)||!isset($channel[$cur])){
				output_program($channel[$next],false,true);
				echo "<td></td><td></td>";
			}elseif(isset($next) && isset($channel[$next])){
				output_program($channel[$cur],($channel[$cur]['stop']==$channel[$next]['start']),false);
				output_program($channel[$next],false,true);
			}else{
				output_program($channel[$cur],false,false);
				echo "<td></td><td></td>";
			}

			echo "</tr>\n";
		}
		echo "</table>";
	}
}else{
    echo "Error found in xmltv file.";
}
?>

